import axios from 'axios'
import emitter from '../utils/eventBus';
import { useLoginStore } from './store'

const service = axios.create({
  baseURL: '/front-api',
  timeout: 100000
})

// 请求拦截器
service.interceptors.request.use(
  config => {
    return config
  },
  error => {
    // 请求错误处理
    console.log('Request Error:', error)
    return Promise.reject(error)
  }
)

// 响应拦截器
service.interceptors.response.use(
  response => {
    // console.log(response)
    // 响应后处理
    if (response.status === 200 && response.data.code !== 401 && response.data.code !== 500) {
      return Promise.resolve(response.data.data)
    } else if (response.data.code === 401) {
      // Redirect to login page when unauthorized
      emitter.emit('open-auth-modal')
      useLoginStore().logout()
      return Promise.reject(response.data.msg)
    } else {
      return Promise.reject(response.data.msg)
    }
  },
  error => {
    console.log('Response Error:', error)
    // Check if the error is a 401 unauthorized error
    if (error.response && error.response.status === 401) {
      // useLoginStore().logout()
      // emitter.emit('open-auth-modal')
    }
    return Promise.reject(error)
  }
)

/**
 * 登录
 * @param {Object} option 
 * @returns 
 */
export const login = (option) => {
  return service.post('/login', option)
}

/**
 * 发送短信验证码
 * @param {String} mobile
 * @returns 
 */
export const sendSms = (mobile) => {
  return service.post('/sendSms',mobile,{
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
  })
}
export const sendLoginSms = (mobile) => {
  return service.post('/sendLoginSms',mobile,{
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
  })
}

/**
 * 获取省市列表
 * @returns 
 */
export const getProvince = (level, pid) => {
  return service.get('/list', {
    params: {
      level: level || 1,
      pid: pid || 100000
    }
  }
)
}

/**
 * 注册
 * @param {Object} option 
 * @returns 
 */
export const register = (option) => {
  return service.post('/register', option)
}

/**
 * 获取banner
 * @returns 
 */
export const banner = () => {
  return service.get('/banner/list')
}

/**
 * 文件上传接口
 * @param {File} file 文件对象
 * @returns 
 */
export const upload = (file) => {
  return service.post('/upload', file, {
    headers: {
      'Content-Type': 'multipart/form-data',
      'Accept': 'application/json'
    }
  })
}

/**
 * 新增作品
 * @param {Object} option 
 * @returns 
 */
export const addProgram = (option) => {
  return service.post('/program/add', option)
}

/**
 * 作品列表
 * @param {Object} option 查询条件
 * @returns 
 */
export const program = (option) => {
  return service.get('/program/list', {
    params: option
  })
}

/**
 * 作品/节目单类型列表
 * @returns 
 */
export const programType = () => {
  return service.get('/programType/list')
}

/**
 * 作品/节目单详情
 * @param {Number} id 
 * @returns 
 */
export const programDetail = (id) => {
  return service.get(`/program/${id}`)
}

/**
 * 获取点赞状态
 * @param {Number} id 
 * @returns 
 */
export const voteStatus = (id) => {
  return service.get(`/programLike/get`, {
    params: {programId: id}
  })
}

/**
 * 点赞
 * @param {Number} id 
 * @returns 
 */
export const vote = (id) => {
  return service.post('/programLike/add', {
    programId: id
  })
}

/**
 * 取消点赞
 * @param {Number} id 
 * @returns 
 */
export const disVote = (id) => {
  return service.delete('/programLike/remove', {
    data: {programId: id}
  })
}

/**
 * 获取用户信息
 * @returns
 */
export const userInfo = () => {
  return service.get('/my')
}

export const modifiyUserInfo = (option) => {
  return service.post('/modifyUser', option)
}

/**
 * 创作者地图
 * @returns 用户地图数据
 */
export const userMap = () => {
  return service.get('/userInfo/countByProvinceId')
}

/**
 * 获取省份创作者数据
 * @param {*} provinceId 
 * @returns 
 */
export const userMapDetail = (provinceId) => {
  return service.get(`/userInfo/listByProvinceId/${provinceId}`)
}

/**
 * 添加作品
 * @param {*} option 
 * @returns 
 */
export const addMyVideos = (option) => {
  return service.post('/program/add', option)
}
/**
 * 获取我的作品
 * @returns 
 */
export const myVideos = () => {
  return service.get('/my/program/list')
}